{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Вопросы для повторения__:\n",
    "* Что такое шаблоны в С++ и зачем они нужны?\n",
    "* Сколько здесь будет скомпилировано функций `print`? Какие?\n",
    "\n",
    "```c++\n",
    "template<typename T>\n",
    "void print(const T& x)\n",
    "{\n",
    "    std::cout << x << std::endl;\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    print(5);\n",
    "    print(6);\n",
    "    print(5.0);\n",
    "    print(6.0f);\n",
    "    print(\"Dobrynia\");\n",
    "    print(\"Nikitich\");\n",
    "    print(\"Alesha\");\n",
    "    print(\"Popovich\");\n",
    "    print(std::string(\"Ilya\"));\n",
    "    print(std::string(\"Muromec\"));\n",
    "}\n",
    "```\n",
    "\n",
    "* Сколько раз компилируется шаблон?\n",
    "* Как быть с ODR?\n",
    "* Плюсы и минусы шаблонов?\n",
    "* Пример с лекции:\n",
    "\n",
    "```c++\n",
    "template<int N, typename T>\n",
    "T add_value(T x)\n",
    "{\n",
    "    return x + N;\n",
    "}\n",
    "\n",
    "int a = add_value<5>(100);\n",
    "```\n",
    "\n",
    "* Что здесь происходит? Сколько будет скомпилировано структур F?\n",
    "\n",
    "```c++\n",
    "template<int N>\n",
    "struct F\n",
    "{\n",
    "    static const int value = N * Factorial<N - 1>;\n",
    "};\n",
    "\n",
    "template<>\n",
    "struct F<0>\n",
    "{\n",
    "    static const int value = 1;\n",
    "};\n",
    "\n",
    "int main()\n",
    "{\n",
    "    return F<5>::value;\n",
    "}\n",
    "```\n",
    "\n",
    "* Что в этом примере?\n",
    "\n",
    "```c++\n",
    "template<typename T>\n",
    "const T& min(const T a, const T b)\n",
    "{\n",
    "    if (a < b)\n",
    "        return a;\n",
    "    else\n",
    "        return b;\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    const std::string& s = min<std::string>(\"string 1\", \"string 2\");\n",
    "    std::cout << s << std::endl;\n",
    "}\n",
    "```\n",
    "\n",
    "* Скомпилируется ли такой код?\n",
    "\n",
    "```c++\n",
    "template<typename T>\n",
    "struct Vector3\n",
    "{\n",
    "    T x;\n",
    "    T y;\n",
    "    T z;\n",
    "    \n",
    "    Vector3<T> operator *(const float value)\n",
    "    {\n",
    "        return { x * value, y * value, z * value };\n",
    "    }\n",
    "};\n",
    "\n",
    "std::string f()\n",
    "{\n",
    "    Vector3<std::string> v;\n",
    "    return v.x\n",
    "}\n",
    "```\n",
    "\n",
    "* А такой?\n",
    "\n",
    "```c++\n",
    "template<typename T>\n",
    "struct Vector3\n",
    "{\n",
    "    T x;\n",
    "    T y;\n",
    "    T z;\n",
    "\n",
    "    template<typename U>\n",
    "    Vector3<T> operator *(const U& value)\n",
    "    {\n",
    "        return { x * value, y * value, z * value };\n",
    "    }\n",
    "};\n",
    "\n",
    "std::string f()\n",
    "{\n",
    "    Vector3<std::string> v;\n",
    "    return v.x\n",
    "}\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
